package gold.enterprise;

import lombok.val;

import java.util.*;

/**
 * Created by fanzhenyu02 on 2021/3/6.
 */
public class ZhiJiangLab {

    Map<Integer, Set<Integer>> memo = new HashMap<>();
    Set<Integer> emptyEleSet = new HashSet<>();

    public int totalDegree(int n) {
        int otherCount = 0;
        for (int j = 4; j <= n; j++) {
            otherCount += getDivider(j).size();
        }

        return n + otherCount - 1;
    }

    public Set<Integer> getDivider(int j) {
        if (j <= 3) return emptyEleSet;
        if (memo.containsKey(j)) return memo.get(j);

        int maxMulti = (int) (Math.sqrt(j) + 1), multiCount = 0;
        for (int i = 2; i <= maxMulti; i++) {
            if (j % i == 0) {
                if (i * i == j) {
                    Set<Integer> curRes = new HashSet<>(Arrays.asList(i));
                    memo.put(j, curRes);
                    return curRes;
                }

                Set<Integer> curRes = new HashSet<>();
                curRes.add(i);
                curRes.add(j / i);
                Set<Integer> subRes = getDivider(j / i);
                for (Integer num : subRes) {
                    curRes.add(num * i);
                }

                curRes.addAll(subRes);
                memo.put(j, curRes);
                return curRes;
            }
        }

        return emptyEleSet;
    }


    public int getMinModify(int[][] grid) {
        int minModifyCount = 0, n = grid.length, m = grid[0].length;
        for (int i = 0; i < n / 2; ++i) {
            for (int j = 0; j < m / 2; ++j) {
                int fourSideSum = grid[i][j] + grid[n - 1 - i][j] + grid[i][m - 1 - j] + grid[n - 1 - i][m - 1 - j];
                if (fourSideSum == 2) minModifyCount += 2;
                else if (fourSideSum < 2) minModifyCount += fourSideSum;
                else minModifyCount += (4 - fourSideSum);
            }
        }
        if (n % 2 == 1) {
            for (int j = 0; j < m / 2; ++j) {
                if (grid[n / 2][j] != grid[n / 2][m - 1 - j]) ++minModifyCount;
            }
        }
        if (m % 2 == 1) {
            for (int j = 0; j < n / 2; ++j) {
                if (grid[j][m / 2] != grid[n - 1 - j][m / 2]) ++minModifyCount;
            }
        }

        return minModifyCount;
    }


    public static void main(String[] args) {
        int[] ropes = {1, 2, 3, 4};
        ZhiJiangLab solution = new ZhiJiangLab();
        System.out.println(solution.totalDegree(6));
        for (int i = 1; i < 40; i++) {
            System.out.println(i + " : " + solution.getDivider(i));
        }

//        System.out.println(solution.getDivider(36));
    }
}



